#include "glob.h"

/******
 queue.c

 @author Will Lamond

 An implementation of the ready queue data structure.
 ******/

//int main() {
//	READYQUEUE queue;
//	queueInit(&queue);
//	int i = 1;
//	int j = 2;
//	int k = 3;
//	enqueue(&queue, i);
//	enqueue(&queue, j);
//	enqueue(&queue, k);
//
//	printf("The head element is: %d\n", dequeue(&queue));
//	printf("The head element is: %d\n", dequeue(&queue));
//	printf("The head element is: %d\n", dequeue(&queue));
//	printf("The head element is: %d\n", dequeue(&queue));
//	return 1;
//}

/*enqueue:
 *
 * Place the qnode node with desired index in into the queue q.
 *
 * Usage: enqueue(q, node, in);
 */

void enqueue(READYQUEUE *q, int in) {
	RQNODE *new = malloc(sizeof(RQNODE));
	new->index = in;
	new->next = NULL;
	if (q->head == NULL) {
		q->head = new;
		q->tail = new;
	} else {
		(q->tail)->next = new;
		q->tail = new;
	}
}

/*dequeue:
 *
 * return the index of the head node and remove it from the queue.
 *
 * Usage: dequeue(q);
 */

int dequeue(READYQUEUE *q) {
	if (!(q->head)) {
		printf("Queue is empty!\n");
		return -1;
	} else {
		RQNODE *temp = (q->head);
		int ret;
		q->head = ((q->head)->next);
		ret = temp->index;
		free(temp);
		temp = NULL;
		return ret;
	}
}

void queueInit(READYQUEUE **q) {
	*q = malloc(sizeof(READYQUEUE));
	(*q)->head = NULL;
	(*q)->tail = NULL;
}
